/**This script is the database script.
 * 
 */

var data = {prop_name:$("#property_name").val(),
				street : $("#street").val(),
	 			suburb: $("#suburb").val(),
	 			town :$("#town").val(),
	 			district :$("#district").val(),
	 			date : $("#date").val(),
	 			owner_name : $("#owner_name").val(),
	 			introducer : $("#introducer").val()
        };





/*
var prop_name= $("#property_name").val();
var street = $("#street").val();
var suburb =$("#suburb").val();
var town =$("#town").val();
var district =$("#district").val();
var date =$("#date").val();
var owner_name = $("#owner_name").val();
var introducer =$("#introducer").val();
*/


var db;
//initialize  database
function initDB(){
try{
	if(!window.openDatabase){
	alert('Databases are not supported in this device');
	return;
	}
	else{
		//defining global variables
		
		var shortName = 'database';
		var version ='1.0';
		var displayName = 'property database';
		var maxSize = 65535;
		
		db =window.openDatabase(shortName, version, displayName, maxSize);
		//db.transaction(function (transaction){
			
	
	}
}catch (e){
	// Error handling code goes here. 
	
	alert ("database did'nt created");
    //if (e == INVALID_STATE_ERR) { 

      // Version number mismatch. 
      //alert("Invalid database version.");
      //}
    //else {
    //	alert("Unknown error "+e+"."); 
    //}
    return
   }
  }


//document.addEventListener("deviceready", onDeviceReady, false);
//function onDeviceReady() {
	
//}


//this id called when an error happens in the transaction
errorHandler = function (transaction, error){
	//alert ('Error:' +error.message + 'code:' +error.code);
	return true;
}

//null db data handler
//nullHandler = function (transaction, results){}

function createtable(data){
	try{
		db.transaction(function (transaction){
			transaction.executeSql('CREATE TABLE IF NOT EXISTS Property_detail(property_name TEST NOT NULL, street TEXT, suburb TEXT,town TEXT, district TEXT, date INTEGER, owner_name TEXT, introducer TEXT );', [], errorHandler);
			//alert ('database is created');
			
			transaction.executeSql("INSERT INTO Property_detail(property_name,street,suburb,town, district,date,owner_name,introducer) VALUES (?,?,?,?,?,?,?,?),[$('prop_name').val(), $('prop_name').val(), $('street').val(),$('suburb').val(),$('district').val(),$('date').val(),$('owner_name').val(),$('introducer').val()];", [], errorHandler); 
			//console.log('db created');
			alert ('your data is updated');
		});
	}catch(e){
		alert ("couldn't create the database");
		return;
	}
}


// callback function to retrieve the data from the property_detail table

celebsDataHandler=function(transaction, results) {

  // Handle the results 

  var html = "<ul>"; 

  for (var i=0; i<results.rows.length; i++) { 

    var row = results.rows.item(i); 

    html += '<li>'+row['name']+'</li>\n';

  } 

  html +='</ul>';

  alert(html);

}





// load the currently selected icons

loadCelebs = function() {

  try {



    mydb.transaction(

        function(transaction) {

           transaction.executeSql('SELECT * FROM property_detail ORDER BY prop_name',[], celebsDataHandler, errorHandler);

        });


  } catch(e) {

    alert('no data');

  }
} 



/*
//this is calles when a successful transactiion happen
function successCallBack(){
	alert ('DEBUGGING success');
}

//function nullHandler(){}
*/

/*
if(!winodw.openDatabase){
	//if  not device not support the database
	alert('Databases are not supported in this device');
	return;
}

//this will open the database locally on the device
//if does not exist, it will create it & return a database object stored in variable db

//this line will try to create the table property_detail in the database just created/opened 
db.transaction(function(tx){

	
	//this line creates the table property_detail if it does not exist & 
	//sets up the columns prop_id,prop_name,prospective or not, street, suburb ,town, district, date, owner's name, introducer if available.
	//prop_is column is an auto incrementing column.
	tx.executeSql('CREATE TABLE IF NOT EXISTS Property_detail(prop_id INTEGER NOT NULL PRIMARY KEY,property_name TEST NOT NULL, street TEXT, suburb TEXT, district TEXT, date INTEGER, owner_name TEXT, introducer TEXT )',[],nullHandler,errorHandler);
	},errorHandler,successCallBack); 
} 		
/*
//list the values in the database to the screen using jquery to update the #lbprop element
function ListDBValues(){
if (!winodow.openDatabase){
	alert('Databases are not supported in this deivice');
	}
}
//this line clears out any content in the #lbprop element on the page so that the next few lines will show updated content and not just keep repeating lines 
 $('#lbprop').html('');
 
 //this next section will select all the content from the propert_detail table and then go through it row by row 
 //appending the prop_id,prop_name,prospective or not, street, suburb ,town, district, date, owner's name, introducer to the  #lbprop element on the page 
  db.transaction(function(transaction) { 
    transaction.executeSql('SELECT * FROM prop_detail;', [], 
      function(transaction, result) { 
       if (result != null && result.rows != null) { 
         for (var i = 0; i < result.rows.length; i++) { 
           var row = result.rows.item(i); 
           $('#lbprop').append('<br>' + row.UserId + '. ' + 
 row.FirstName+ ' ' + row.LastName); 
         } 
       } 
      },errorHandler); 
  },errorHandler,nullHandler); 


  return; 
 } 

 
//this is the function that puts values into the database using the values from the text boxes on the screen 
function AddValueToDB(){ 

 if (!window.openDatabase) { 
   alert('Databases are not supported in this browser.'); 
   return; 
 } 
// this is the section that actually inserts the values into the User table 
 db.transaction(function(transaction) { 
   transaction.executeSql('INSERT INTO Property_detail(prop_id, property_name,street,suburb,district,date,owner_name,introducer) VALUES (?,?,?,?,?,?,?,?)',[$('#txprop_name').val(), $('#txprop_name').val(), $('#txstreet').val(),$('#txsuburb').val(),$('#txdistrict').val(),$('#txdate').val(),$('#txowner_name').val(),$('#txintroducer').val()],nullHandler,errorHandler); 
 });                                                                                         

// this calls the function that will show what is in the User table in the database 
 //ListDBValues(); 
 return false; 
}
*/

 
